﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Chemistry.Models;
using Chemistry.Tools;
using GLCommon;
using Newtonsoft.Json.Linq;

namespace Chemistry.ViewModels
{
    using GalaSoft.MvvmLight.Command;

    public class ExamineeLoginViewModel : VmBase
    {
        #region 字段和属性
        private ExamInfo _examInfo;

        private ExamineeRemote _examineeInfo;
        public ExamineeRemote ExamineeInfo
        {
            get { return _examineeInfo; }
            set { _examineeInfo = value; RaisePropertyChanged("ExamineeInfo"); }
        }

        private bool _isInputover = false;
        /// <summary>
        /// 是否输入完准考证
        /// </summary>
        public bool IsInputOver
        {
            get { return _isInputover; }
            set
            {
                _isInputover = value;
                RaisePropertyChanged("IsInputOver");
            }
        }

        private bool _isinputing = true;
        /// <summary>
        /// 是否在输入准考证
        /// </summary>
        public bool IsInputing
        {
            get { return _isinputing; }
            set
            {
                _isinputing = value;
                RaisePropertyChanged("IsInputing");
            }
        }

        private bool _isEnter = true;
        /// <summary>
        /// 是否在确认准考证
        /// </summary>
        public bool IsEnter
        {
            get { return _isEnter; }
            set
            {
                _isEnter = value;
                RaisePropertyChanged("IsEnter");
            }
        }

        private string _examNumber;
        /// <summary>
        /// 准考证号
        /// </summary>
        public string ExamNumber
        {
            get { return _examNumber; }
            set { _examNumber = value; RaisePropertyChanged("ExamNumber"); }
        }

        private string _version = "v0.0.01";
        /// <summary>
        /// 版本号
        /// </summary>
        public string Version
        {
            get { return _version; }
            set { _version = value; RaisePropertyChanged("Version"); }
        }


        private ImageSource _imageSource;

        public ImageSource ImageSource
        {
            get { return _imageSource; }
            set { _imageSource = value; RaisePropertyChanged("ImageSource"); }
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; RaisePropertyChanged("Name"); }
        }

        private string _idcard;

        public string IdCard
        {
            get { return _idcard; }
            set { _idcard = value; RaisePropertyChanged("IdCard"); }
        }

        private string _worktype;

        public string WorkType
        {
            get { return _worktype; }
            set { _worktype = value; RaisePropertyChanged("WorkType"); }
        }

        #endregion

        #region 命令
        public RelayCommand BeginExamCommand { get; private set; }
        public RelayCommand ReturnCommand { get; private set; }
        public RelayCommand LoginCommand { get; private set; }
        #endregion

        public ExamineeLoginViewModel()
        {
            BeginExamCommand = new RelayCommand(BeginExamExec, ()=> _isEnter);
            ReturnCommand = new RelayCommand(ReturnExec);
            LoginCommand = new RelayCommand(LoginExec);

            Register<string>(this, MessageToken.SendNumberText, (s =>
                {
                    ExamNumber = s; 
                }));

            Register<object>(this, MessageToken.BackLogin, i =>
            {
                ClearBack();
            });

        }

        /// <summary>
        /// 考生登录
        /// </summary>
        private async void LoginExec()
        {
           _examInfo =await Login();
           if (_examInfo != null)
            {
                IsInputOver = true;
                IsInputing = false;
            }
        }

     
        /// <summary>
        /// 确认开始考试
        /// </summary>
        private async void BeginExamExec()
        {
            _isEnter = false;
            Send<ExamInfo>(_examInfo, MessageToken.ShowGuideWindow);
            _isEnter = true;
        }

        private void ReturnExec()
        {
            ClearBack();
        }

        private void ClearBack()
        {
            ExamNumber = String.Empty;
            ExamineeInfo = null;
            IsInputing = true;
            IsInputOver = false;
            Send(MessageToken.ClearNumberText);
        }
        

        /// <summary>
        /// 输入准考证
        /// </summary>
        /// <returns></returns>
         private async Task<ExamInfo> Login()
        {
            ExamInfo examInfo = null;
            Send<string>("获取考试信息中......", MessageToken.ShowExamLoadMessage);
            string url = string.Format("oplogin?nr={0}&km=k2",ExamNumber);

            var result = await HttpTool.GetAsync<ExamineeRemote>(url);
            await Task.Delay(1000);
            Send<string>("获取考生信息中......", MessageToken.CloseExamLoadMessage);
            if (result != null)
            {
                string loginMsg = String.Empty;
                switch (result.status)
                {
                    case 0:
                        Examinee  examinee = new Examinee(){ Name =  result.name, TicketId = result.nr};
                        string info = string.Format("考生{0}登录", examinee.Name);
                        LogTool.WriteInfoLog(info);
                        examInfo = new ExamInfo()
                        {
                             ExamineeRemote = result,
                              Examinee =  examinee
                        };
                        WorkType = result.wtadded;
                        IdCard = result.idcard;
                        this.Name = result.name;
                        if (!string.IsNullOrEmpty(result.photo))
                        {
                            examinee.Pictrue = Convert.FromBase64String(result.photo);
                            ImageSourceConverter converter = new ImageSourceConverter();
                            this.ImageSource = (ImageSource) converter.ConvertFrom(examinee.Pictrue);
                        }
                        else
                        {
                            this.ImageSource =
                                new BitmapImage(new Uri(@"../Images/nophoto.jpg", UriKind.RelativeOrAbsolute));
                        }
                        IsOpen = false;
                        break;
                    case 1:
                        loginMsg = "准考证不存在";
                        break;
                    case 2:
                        loginMsg = "考试计划没有审批同意";
                        break;
                    case 3:
                        loginMsg = "考试时间不正确";
                        break;
                    case 4:
                        loginMsg = "触屏机自助考试没有完成";
                        break;
                    case 5:
                        loginMsg = "考试成绩已合格";
                        break;
                    case 6:
                        loginMsg = "考生补考未合格";
                        break;
                    case 7:
                        loginMsg = "已有k1/k2/k3成绩";
                        break;
                    case 8:
                        loginMsg = "未分配k1/k2/k3考试项";
                        break;
                    case -1:
                        loginMsg = "考试进度错误";
                        break;
                    case -3:
                        loginMsg = "服务器内部错误，请联系管理员";
                        break;
                }
                if (result.status != 0)
                {
                    await Task.Delay(1000);
                    Send<string>(loginMsg, MessageToken.ShowExamMessage);
                }
            }
            else
            {
                Send<string>("获取考试信息中......", MessageToken.CloseExamLoadMessage);
                await Task.Delay(1000);
                Send<string>("未找到考生信息", MessageToken.ShowExamMessage);
            }
            return examInfo;
        }
    }
}
